<?php

/**
 * 
 * 数据库的操作工具类
 * @author Administrator
 *
 */
class DatabseUtils{
	private $config;
	/**
	 * 
	 * 
	 * @param $configFilePath 配置文件路径
	 */
	function __construct($configFilePath){
		//$this->config=include '../config/config_database.php';
		$this->config=include $configFilePath;
	}
	/**
	 * 
	 * 读取配置文件
	 * @param string $nameStr 配置名称
	 */
	public function get($nameStr){
		$config=$this->config;
		 return $config[$nameStr];
	}
	
	/**
	 * 打开数据库连接
	 * @return string 数据库连接
	 */
	public function openConn(){
		//读取数据库配置
		$db_type=$this->get("DB_TYPE");
		$db_host=$this->get("DB_HOST");
		//数据库名称
		$db_name=$this->get("DB_NAME");
		//用户名
		$db_user=$this->get("DB_USER");
		$db_pwd=$this->get("DB_PWD");
		$db_port=$this->get("DB_PORT");
		$db_charset=$this->get("DB_CHARSET");
		$conn=null;
		//打开数据库连接
		if($db_type=="mysql"){
			$conn=mysql_connect($db_host.":".$db_port,$db_user,$db_pwd) or die("Unable to connect to the MySQL!");
			//选择数据库
			mysql_select_db($db_name,$conn);
		}elseif ($db_type=="oracle"){
			$conn=oci_connect($db_user,$db_pwd,$db_host.":".$db_port."/".$db_name) or die("Unable to connect to the Oracle!");
		}else{
			$conn=null;
		}		
		return $conn;
	}
	
	/**
	 * 关闭数据库连接
	 * @param resource $conn
	 */
	public function closeConn($conn){
		$db_type=$this->get("DB_TYPE");
		if($db_type=="mysql"){
			mysql_close($conn);
		}elseif($db_typ=="oracle"){
			oci_close($conn);
		}
	}
	
	public function query($conn,$query){
	   $query="select * from bsm.gbsm where bag_id=:bag_id";
       $statement=oci_parse($conn,$query);
       $v="999cc9dd-ddcc-4cd6-bfab-f43763a07a0a";
       oci_bind_by_name($statement,":bag_id",$v);
       if($statement!=false){
       		//执行语句
      	    $flag=oci_execute($statement);
      	    if($flag!=false){
      	    	$flag=oci_fetch_all($statement,$result);
      	    	//获取查询结果
      	    	if($flag!=false){
      	    		//print_r($result) ;
      	    		foreach ($result as $field=>$valueArray){
      	    			echo $field.":".$valueArray[0]."<br/>";
      	    		}
      	    		
      	    	}else{
      	    		$e = oci_error($statement); 
       				echo  $e['message']; 
      	    	}
      	    }else{
      	    	$e = oci_error($statement); 
       			echo  $e['message']; 
      	    }
       }else{
       		$e = oci_error($conn); 
       		echo  $e['message']; 
       }
     //释放资源
       oci_free_statement($statement);
       oci_close($conn);
		
	}
	
	/**
	 * 
	 * mysql中获得所有记录数
	 * @return Array 记录总数，每条记录为数组，索引为字段名
	 */
	public function queryAll_mysql($query){
		$conn=$this->openConn();
		//$query="select * from user where id=4";
		//编译sql语句
		$statement=mysql_query($query,$conn);
		//获得结果
		$records=Array();
		while($result=mysql_fetch_array($statement)){
			array_push($records, $result);
			//print_r($result);
		}
		$this->closeConn($conn);
		return $records;
	}
	
	public function count_mysql($query){
		$conn=$this->openConn();
		//编译sql语句
		$statement=mysql_query($query,$conn);
		//获得结果
		$count=mysql_fetch_array($statement) or die(mysql_error($conn));
		$this->closeConn($conn);
		return $count[0];
	}
	
	/**
	 * 
	 * mysql的写操作：插入、修改和删除
	 */
	public function operate_mysql($query){
		$conn=$this->openConn();
		//编译sql语句
		$statement=mysql_query($query,$conn) or die(mysql_error($conn));
		oci_free_statement($statement);
        oci_close($conn);
	}
	/**
	 * 
	 * oracle查询所有记录数
	 * @param string $query 查询sql
	 */
	public function queryAll_oracle($query){
	   $conn=$this->openConn();
	   //编译sql语句
       $statement=oci_parse($conn,$query) or die(oci_error($statement));
       //执行sql语句
       $flag=oci_execute($statement) or die(oci_error($statement));
       //总的记录数
       $count=oci_fetch_all($statement,$result) or die(oci_error($statement));
       //获得每条记录的各个字段的值
       $records=Array();
       for($i=0;$i<$count;$i++){
           $record=Array();
           foreach ($result as $field=>$valueArray){
           //echo $field.":".$valueArray[$i]."<br/>";
           $record[$field]=$valueArray[$i];
       }
        array_push($records, $record);
        }
       // print_r($records);
       //释放资源
       oci_free_statement($statement);
       oci_close($conn);
       return $records;
	}
	
	/**
	 * 
	 * oracle的写操作:插入、修改和删除
	 * @param string $query
	 */
	public function operate_oracle($query){
	   $conn=$this->openConn();
	   //编译sql语句
       $statement=oci_parse($conn,$query);
       //处理异常       
       $this->error_oracle($conn);
       //执行sql语句
       $count=oci_execute($statement);
       
       //处理异常
        $this->error_oracle($conn);
	}
	
	/**
	 * 
	 * oracle中处理异常
	 * @param unknown_type $conn
	 */
	private function error_oracle($conn){
	   $e=oci_error($conn);
       if($e){
          die($e["message"]);
       }
	}
	/**
	 * 
	 * 获得总的记录数
	 * @param unknown_type $result 数据库返回的结果
	 */
	private function getCount($result){
		$count=0;
		foreach ($result as $fieldValueArray){
          //总记录数
          $fieldValueArray_length=count($fieldValueArray);
          $count=$fieldValueArray_length;
          break;
		}
		return $count;
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}

?>